//
//  File.swift
//
//
//  Created by didi on 2022/9/9.
//

/// generic api component
public protocol ObjectComponent {
    /// path of the given component for the API endpoint
    static var pathKey: String { get }

    /// permission key (context or namespace)
    static var permissionKey: String { get }

    /// key to locate the assets
    static var assetKey: String { get }

    /// create a new permission object for a given action
    static func permission(for action: Permission.Action) -> Permission

    /// list the available permissions for a given component (by default CRUD including patch)
    static func availablePermissions() -> [Permission]
}

public extension ObjectComponent {
    static var pathKey: String { String(describing: self).lowercased() }
    static var permissionKey: String { String(describing: self).lowercased() }
    static var assetKey: String { pathKey }

    static func availablePermissions() -> [Permission] {
        Permission.Action.crud.map { permission(for: $0) }
    }
}
